//取消预约单
$('#btn_cancel').click(function(){
	var id = $('#id').val();
	$.teninedialog({
	    title:'系统提示',
	    content:'确认取消本条预约?',
	    showCloseButton:false,
	    otherButtons:["确定","取消"],
	    otherButtonStyles:['btn-primary','btn-white'],
	    bootstrapModalOption:{keyboard: true},
	    clickButton:function(sender,modal,index){
	   	 //取消预约单
	   	 if(index == 0){
	   		    json = "{\"id\":\""+id+"\"}";
	            $.ajax({
	            	type:"post",
	                contentType:"application/json; charset=UTF-8",
	                url: ctx + "/appointment/cancel",
	                data:json,
	                dataType:"json",
	                success: function(msg){
	                	$.teninedialog({
	                        title:'系统提示',
	                        content:msg.message,
	                        showCloseButton:false,
	                        otherButtons:["确定"],
	                        otherButtonStyles:['btn-primary'],
	                        bootstrapModalOption:{keyboard: true},
	                        clickButton:function(sender,modal,index){
	                        	location.reload();
	                        }
	                    });
	                },
	                error: function (msg) {
	                	bootbox.alert(msg.message);
	                }
	            });
	   	 }
	   	 $(this).closeDialog(modal); 
	    }
	});
});


$("#add_relation").click(function(){
	$('#rela_content').val("");
});

//增加联系记录
$('#btn_add_relation').click(function(){
	var investorId = $('#investorId').val();
	var subId = $('#id').val();
	var content = $('#rela_content').val();
	if(content == ""){
		bootbox.alert('联系记录不能为空!');
		return;
	}
	var json = "{\"investorId\":\""+investorId+"\",\"content\":\""+content+"\",\"subId\":\""+subId+"\"}";
	$.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/investor/addRela",
        data:json,
        dataType:"json",
        success: function(msg){
        	$('#dsimiss_add_relation').click();
        	$('#rela_content').val('');
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        },
        error: function (msg) {
        	$('#dsimiss_add_relation').click();
        	$.teninedialog({
                title:'系统提示',
                content:"服务器偷了个小懒,查询预约失败了！",
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        }
    });
});

//预约单处理记录
$('#btn_deal_appointment').click(function(){
	var id = $('#id').val();
	var opinion = $('#deal_content').val();
	if(opinion == null ||opinion == ''){
		$('#dsimiss_deal_appointment').click();
		bootbox.alert('处理意见不能为空!');
		return;
	}
	var json = "{\"opinion\":\""+opinion+"\",\"id\":\""+id+"\"}";
	$.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/appointment/deal",
        data:json,
        dataType:"json",
        success: function(msg){
        	$('#dsimiss_deal_appointment').click();
        	$('#deal_content').val('');
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        },
        error: function (msg) {
        	$('#dsimiss_deal_appointment').click();
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        }
    });
});


$('#btn_allot').click(function(){
	var v = $('#allot_div').css('display');
	if(v === 'none'){
		$('#allot_div').css('display','block');
	}else{
		$('#allot_div').css('display','none');
	}
});


//Autocomplete
$("#tags").autocomplete({
    source: function(query,process){
        $.post(ctx + "/investor/employee/"+query.term,{"matchCount":200},function(data){
        	var ebean = []
        	for (var int = 0; int < data.length; int++) {
        		var temp = [];
        		temp.label = data[int].employeeName +"  "+ data[int].employeeNo;
        		temp.value = data[int].employeeNo;
        		temp.id = data[int].employeeId;
        		temp.name = data[int].employeeName;
        		ebean.push(temp);
    		}
            return process(ebean);
        });
    },
    search:function(event, ui){
    	$("#employeeName").html('');
        $("#employeeNo").html('');
        $("#employeeId").val('');
    },
    select: function(event, ui) {
        $("#tags").val(ui.item.label);
        $("#employeeName").html(ui.item.name);
        $("#employeeNo").html(ui.item.value);
        $("#employeeId").val(ui.item.id);
        var uistr =ui.item;
        // 阻止事件的默认行为
        event.preventDefault(); 
    }
});

//预约单分配
$('#btn_confirm_allot_employee').click(function(){
	var employeeId = $('#employeeId').val();
	var employeeName = $('#employeeName').html();
	var investorName = $('#name').html();
	var oldId = $('#oldEmployeeId').val();
	var belongEmpId = $('#belongEmpId').val();
	var id = $('#id').val();
	if (employeeId == null || employeeId == '') {
		bootbox.alert("请选择理财师！");
		return;
	}
	if(oldId == employeeId){
		bootbox.alert('该预约单属于这位理财师,不能重复分配给他.');
		return;
	}
	if(employeeId != belongEmpId && belongEmpId != ""){
		bootbox.alert('该理财师并非投资人所对应的理财师,可以通知团队长修改投资人所属理财师.');
		return;
	}
	json = "{\"employeeId\":\""+employeeId+"\",\"id\":\""+id+"\",\"investorName\":\""+investorName+"\",\"employeeName\":\""+employeeName+"\"}";
	$.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/appointment/allotEmp",
        data:json,
        dataType:"json",
        success: function(msg){
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        },
        error: function (msg) {
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        }
    });
});


var AppointmentModule = function () {
    var editable = function () {
        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                    "lengthMenu": [
                        [10, 15, 20, 50, -1],
                        [10, 15, 20, 50, "All"]
                    ],
                    "autoWidth":false,
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [0]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [1]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [2]
						}         
                    ],
                    "columns": [
                       { "data": "employeeName" },
                       { "data": "content" },
                       { "data": "visitTime" }
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/appointment/queryRela",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"page":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var subId = $('#id').val();
	                   var form = "{\"id\":\""+subId+"\"}";
                   	   var js = form.replace('}', ',');
                   	   json = js + json + "}";
                       $.ajax({
                       	   "contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ctx + "/appointment/queryRela",
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                $('#btn_query').click(function(){
                	oTable.fnClearTable();
                });
            }

        };
    }();
    return {
        init: function () {
        	editable.init();
        }
    };
}();